package org.lsms.servlet;

/**
 * login servlet
 * @author ZhanXueliang
 * 2010.8.7
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lsms.bean.*;
import org.lsms.dao.impl.*;

public class login extends HttpServlet {

	public void destroy() {
		super.destroy(); 
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = new User();
		//user.setFirstname(request.getParameter("firstname"));
		String loginname = request.getParameter("loginname");
		user.setLoginname(request.getParameter("loginname"));
		user.setPassword(request.getParameter("password"));
		user.setPermission(Integer.parseInt(request.getParameter("permission")));
		
		HttpSession session = request.getSession();
		DaoImpl impl = new DaoImpl();
		impl.update_last_login(user);
		
		if(impl.login(user)){
			//session.setAttribute("userid", user.getUserid());
			session.setAttribute("username", user.getLoginname());
			session.setAttribute("userid", impl.get_user_id(loginname));
			session.setAttribute("permission", user.getPermission());
			session.setAttribute("logintimes", impl.get_user_login_times(loginname));
			
			int permission = user.getPermission();
			switch(permission){
			case 0:
				response.sendRedirect("chair/chair_main.jsp");
				break;
			case 1:
				if(impl.get_user_login_times(loginname)==0){
					response.sendRedirect("member/member_welcome.jsp");
				}else{
					response.sendRedirect("member/member_main.jsp");
				}
				
				break;
			case 2:
				if(impl.get_user_login_times(loginname)==0){
					response.sendRedirect("reviewer/reviewer_welcome.jsp");
				}else{
					response.sendRedirect("reviewer/reviewer_main.jsp");
				}
				
				break;
			case 3:
				response.sendRedirect("author/author_main.jsp");
				break;
			case 4:
				response.sendRedirect("admin/admin_main.jsp");
			}
		}
		else{
			response.sendRedirect("error/login_fail.jsp");
		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}


	public void init() throws ServletException {

	}

}
